var app = getApp();
const $ = require('../../../utils/utils.js')
const config = require('../../../config.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cardList: "",
        zwtShow: false,
        activityId: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log('options----', options)
        let activityId = options.activityId
        this.setData({
            activityId: activityId
        })
    },

    onShow: function () {

        var that = this;
        console.log("getMonthCard_onShow()")
        that.inviteEvent(that.data.activityId);
        that.getActivityCurrentInfo(that.data.activityId);

    },


    //查看活动列表每个活动进行结果
    getActivityResult: function (e) {
        $.goto(config.page.invitorShareResult + '?hdCardInitiatorId=' + e.currentTarget.dataset.id, false)
    },

    getActivityCurrentInfo: function (activityId) {
        var that = this;
        let data = {
            usertoken: wx.getStorageSync("usertoken"),
            userid: wx.getStorageSync("userid"), 
            mobile: wx.getStorageSync("userPhone"),
            activityId: activityId
        }

        let url = $.getUrl(config.getCardActivityCurrentInfo, data)
        $.get(url).then(data => {
            console.log("getActivityCurrentInfo()",data)
            if (data.code == 0) {
                if (data.detail.initiators == null || data.detail.initiators.length == 0 ) {
                    that.setData({
                        zwtShow: true
                    });
                } else {
                    that.setData({
                        cardList: data.detail.initiators
                    });
                }

            }
        })
    },

    // 发起邀请
    inviteEvent: function (activityId) {
        var that = this;
        let data = {
            usertoken: wx.getStorageSync("usertoken"),
            userid: wx.getStorageSync("userid"),
            mobile: wx.getStorageSync("userPhone"), 
            activityId: activityId
        }
        let url = config.cardShareInitiator
        $.post(url, data).then(data => {
            console.log("inviteEvent()", data)
            if (data.code == 0) {
                wx.setStorageSync("hdCardInitiatorId", data.detail.initiator.hdCardInitiatorId);
            }
        })

    },




    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        let that = this
        console.log("buttom", res);
        if (res.from === 'button') {
            // 来自页面内转发按钮
            console.log(res.target);
        }
        return {
            title: '帮我抢月卡，免费读绘本',
            imageUrl: "../../../img/loadIcon.png",
            path: config.page.invitorShareResult + '?hdCardInitiatorId=' + wx.getStorageSync("hdCardInitiatorId") + "&activityId=" + that.data.activityId,
            success: function (res) {
                console.log("分享成功", wx.getStorageSync("hdCardInitiatorId"));
                console.log('分享成功---activityId', that.data.activityId)
            },
            fail: function (res) {
                // 转发失败
                console.log("fail");
            }
        }
    }
})